package clienttest.common;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JTable;
import com.itextpdf.text.pdf.BaseFont;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author lekhanhduy
 */
public class PrintToPdf {

    private static String FILE = "Lich_su_giao_dich_";
    private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 14,
            Font.BOLD);
    private static Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12,
            Font.BOLD);
    private JTable jTable;
    private String[] header;
    private String name;
    BaseFont unicode;

    public PrintToPdf(JTable jtb, String[] head, String name) {
        this.jTable = jtb;
        this.header = head;
        this.name = name;
    }

    public void printfPDF(String fromDate, String toDate) {
        try {
            Document document = new Document();
            SimpleDateFormat dt = new SimpleDateFormat("dd_MM_yyyy_hh_mm_ss");
            String date = dt.format(new Date());
            PdfWriter.getInstance(document, new FileOutputStream("C:/Users/lekhanhduy/Desktop/" + FILE + date + ".pdf"));
            document.open();
            try {
                unicode = BaseFont.createFont("c:/windows/fonts/Arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            } catch (IOException ex) {
                Logger.getLogger(PrintToPdf.class.getName()).log(Level.SEVERE, null, ex);
            }
            Paragraph paragraph = new Paragraph();
            // tao 1 dong trong
            addEmptyLine(paragraph, 1);
            if (name != null) {
                paragraph.add(new Paragraph(new String(("Lịch sử giao dịch của " + name).getBytes("UTF-8"), "UTF-8"),
                        new Font(unicode, 14)));
                addEmptyLine(paragraph, 1);
            }else
            {
                 paragraph.add(new Paragraph(new String(("Lịch sử giao dịch").getBytes("UTF-8"), "UTF-8"),
                        new Font(unicode, 14)));
                addEmptyLine(paragraph, 1);
            }
            if (!fromDate.equals(toDate)) {
                paragraph.add(new Paragraph(new String(("Thời gian từ ngày : " + fromDate + " đến ngày : " + toDate).getBytes(), "UTF-8"),
                        new Font(unicode, 12)));
            } else {
                paragraph.add(new Paragraph(new String(("Ngày: " + fromDate).getBytes(), "UTF-8"),
                        new Font(unicode, 12)));
            }
            addEmptyLine(paragraph, 1);

            int length = header.length;
            int count = jTable.getRowCount();
            if (length > 0 && count > 0) {
                PdfPTable table = new PdfPTable(length);
                for (int i = 0; i < length; i++) {
                    String string = header[i];
                    PdfPCell c = new PdfPCell(new Paragraph(new String(string.getBytes(), "UTF-8"), new Font(unicode, 12)));
                    c.setHorizontalAlignment(Element.ALIGN_CENTER);
                    table.addCell(c);
                }
                for (int i = 0; i < count; i++) {
                    for (int j = 0; j < length; j++) {
                        Object obj = GetData(jTable, i, j);
                        table.addCell(new Paragraph(new String(obj.toString().getBytes(), "UTF-8"), new Font(unicode, 10)));
                    }
                }
                paragraph.add(table);
            }
            document.add(paragraph);
            document.close();
        } catch (DocumentException ex) {
        } catch (FileNotFoundException ex) {
        } catch (UnsupportedEncodingException ex) {
        }
    }

    private Object GetData(JTable table, int row_index, int col_index) {
        return table.getModel().getValueAt(row_index, col_index);
    }

    private static void addEmptyLine(Paragraph paragraph, int number) {
        for (int i = 0; i < number; i++) {
            paragraph.add(new Paragraph(" "));
        }
    }
}
